<?php

class Install extends Kernel {

    private $step = null;
    public $t = null;

    public function __construct() {
        $this->t = Template::getInstance();
        $this->t->init( WEBROOT . "themes/install/", WEBTPL . "install/" );
        $this->step = G( "step", 1 );
    }

    public function index( $a ) {
        include $this->t->display( $a );
    }

    public function checkDB() {
        echo $this->conn_test( $_POST );
    }

    public function install() {
        $config = "<?php \n";
        foreach ( G( "db" ) as $k => $v ) {
            define( $k, trim( $v ) );
            $config .= "define('{$k}', '{$v}');\n";
        }
        $key = md5( uniqid() );
        $cname = substr( $key, 8, 6 );
        $config .= "define('WEBKEY', '" . $key . "');\n";
        $config .= "define('WEBCOOKIE', '" . $cname . "');\n";
        define( "WEBKEY", $key );

        $this->connection();

        $this->create_table( "table.sql" );
        $this->create_table( "data.sql" );
        $this->show( "导入数据    ... 成功" );


        $this->table = "user";
        foreach ( $_POST[ 'user' ] as $k => $v ) {
            $users[ $k ] = trim( $v );
        }
        $users[ 'sid' ] = 1;
        $users[ 'actions' ] = '*';
        $users[ 'pass' ] = md6( $users[ 'pass' ] );
        $this->create( $users ) && $this->show( "创建管理员     ... 成功" );

        $this->Cache()->update() && $this->show( "更新缓存     ... 成功" );

        $configFile = WEBROOT . "system/config.php";
        @file_put_contents( $configFile, $config ) && $this->show( "创建配置文件     ... 成功" );
        @chmod( $configFile, MODE );

        echo "安装完成     ... 成功<br />";
    }

    private function show( $msg ) {
        echo $msg . "<br />";
    }

    private function create_table( $file ) {
        $table_sql = file_get_contents( WEBROOT . 'install/data/' . $file );
        $table_sql = trim( str_replace( 'xx_', dbprefix, $table_sql ) );
        $sqldump = explode( ";\r", $table_sql );
        //array_pop( $sqldump );

        foreach ( $sqldump as $k => $v ) {
            $sqldump[ $k ] = trim( preg_replace( "/\r/", "", $v ) );
        }

        # 创建数据库
        foreach ( $sqldump as $query ) {
            if ( substr( $query, 0, 12 ) == 'CREATE TABLE' ) {
                preg_match( "/CREATE TABLE `([a-z0-9_]+?)`/is", $query, $m );
                $name = $m[ 1 ];
                $this->show( "创建表: {$name}     ... 成功" );
            }
            $this->runSql( trim( $query ) );
        }
        return true;
    }

    private function checkDIR( $dir ) {
        $writeable = 0;
        if ( !is_dir( $dir ) ) {
            @mkdir( $dir, 0777 );
        }
        if ( is_dir( $dir ) ) {
            if ( $fp = @fopen( "$dir/test.txt", 'w' ) ) {
                @fclose( $fp );
                @unlink( "$dir/test.txt" );
                $writeable = 1;
            } else {
                $writeable = 0;
            }
        }
        return $writeable;
    }

    private function conn_test( $rows ) {

        foreach ( $rows as $k => $v ) {
            define( $k, trim( $v ) );
        }

        if ( !$conn = @mysql_connect( dbhost, dbuser, dbpass ) ) {
            return "Can not connect to MySQL server : " . mysql_error();
        }

        if ( !$db = mysql_select_db( dbname, $conn ) ) {
            return "Can\'t use " . dbname . " : " . mysql_error();
        }

        if ( $query = @mysql_query( "SHOW TABLES FROM " . dbname ) ) {
            while ( $row = mysql_fetch_row( $query ) ) {
                if ( preg_match( "/^" . dbprefix . "/", $row[ 0 ] ) ) {
                    @mysql_close( $conn );
                    return 'unique';
                }
            }
        }

        @mysql_close( $conn );
        return 'success';
    }
}